package info.thereisonlywe.onenesspathfinder.ui;

import info.thereisonlywe.core.project.ProjectConstants;
import info.thereisonlywe.core.project.ProjectLocalization;
import info.thereisonlywe.core.toolkit.GUIToolkit;
import info.thereisonlywe.quran.Quran;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GoToVerseDialog {

	private static JDialog dialog;
	private static JButton okButton;
	private static JPanel panel;
	private static final JPanel panel_1 = new JPanel();
	private static final JTextField textField = new JTextField();
	private static final JTextField textField_1 = new JTextField();
	private static final JLabel lblNewLabel = new JLabel(":");

	/**
	 * @wbp.parser.entryPoint
	 */
	private static void init()
	{
		textField.setHorizontalAlignment(JLabel.CENTER);
		textField_1.setHorizontalAlignment(JLabel.CENTER);
		textField_1.setPreferredSize(new Dimension(38, 28));
		textField.setPreferredSize(new Dimension(38, 28));
		if (dialog != null) return;
		dialog = new JDialog();
		dialog.setResizable(false);
		dialog.setIconImage(null);
		dialog.setTitle(ProjectLocalization.getString(
				ProjectConstants.ONENESS_PATHFINDER_PROJECT, "GoToVerse"));
		dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		dialog.setPreferredSize(new Dimension(350, 200));
		dialog.setSize(new Dimension(120, 100));
		dialog.setModal(true);
		dialog.getContentPane().setLayout(new BorderLayout());
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 0));
		dialog.getContentPane().add(buttonPane, BorderLayout.SOUTH);

		okButton = new JButton(ProjectLocalization.getString(
				ProjectConstants.CORE_PROJECT, "OK"));
		okButton.setActionCommand("OK");
		dialog.getRootPane().setDefaultButton(okButton);
		buttonPane.add(okButton);

		panel = new JPanel();
		dialog.getContentPane().add(panel, BorderLayout.CENTER);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(
				Alignment.LEADING).addComponent(panel_1,
				GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE));
		gl_panel.setVerticalGroup(gl_panel.createParallelGroup(
				Alignment.LEADING).addComponent(panel_1,
				GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE));

		panel_1.add(textField);

		panel_1.add(lblNewLabel);

		panel_1.add(textField_1);
		panel.setLayout(gl_panel);
		initListeners();
	}

	private static void initListeners()
	{

		KeyboardFocusManager.getCurrentKeyboardFocusManager()
				.addKeyEventDispatcher(new KeyEventDispatcher()
				{
					@Override
					public boolean dispatchKeyEvent(KeyEvent e)
					{
						boolean keyHandled = false;
						if (e.getID() == KeyEvent.KEY_PRESSED)
						{
							if (e.getKeyCode() == KeyEvent.VK_ESCAPE
									&& dialog.isVisible())
							{
								dialog.setVisible(false);
								UIDelegate.setOpenDialog(false);
								keyHandled = true;
							}
						}
						if (e.getID() == KeyEvent.KEY_RELEASED
								&& dialog.isVisible())
						{
							if (e.getKeyCode() == KeyEvent.VK_ENTER)
							{
								okButton.doClick();
								keyHandled = true;
							}
						}
						return keyHandled;
					}
				});

		okButton.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				try
				{
					int sNo = Integer.parseInt(textField.getText());
					int vNo = Integer.parseInt(textField_1.getText());
					dialog.setVisible(false);
					if (Quran.isAValidVerse(sNo, vNo))
					{
						UIDelegate.seekToVerse(Quran.getVerse(sNo, vNo));
						UIDelegate.setOpenDialog(false);
					}
					else
					{
						UIDelegate.showErrorDialog("NoSuchVerse");
					}
				}
				catch (NumberFormatException ex)
				{
					dialog.setVisible(false);
					UIDelegate.showErrorDialog("NoSuchVerse");
				}
			}
		});

		dialog.addWindowListener(new WindowAdapter()
		{

			@Override
			public void windowClosing(WindowEvent e)
			{
				dialog.setVisible(false);
				UIDelegate.setOpenDialog(false);
			}
		});

	}

	public static void showDialog()
	{
		init();
		dialog.setLocation(GUIToolkit.getCenteredWindowPosition(
				dialog.getWidth(), dialog.getHeight()));
		UIDelegate.setOpenDialog(true);
		dialog.setVisible(true);
	}
}
